package com.example.bbs.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bbs.R;
import com.example.bbs.util.DisplayUtil;

public class CheckAlertDialog extends AlertDialog {

    static class AlertParams {
        public boolean mCancelable;
        public CharSequence mTitle;
        public CharSequence mMessage;
        public CharSequence mPositiveText;
        public CharSequence mNegativeText;
        public View.OnClickListener mPositiveListener;
        public View.OnClickListener mNegativeListener;
    }

    public static class Builder extends AlertDialog.Builder {

        AlertParams P = new AlertParams();
        CheckAlertDialog dialog;

        public Builder(Context context) {
            super(context);
        }

        @Override
        public Builder setCancelable(boolean cancelable) {
            P.mCancelable = cancelable;
            return this;
        }

        @Override
        public Builder setTitle(CharSequence title) {
            P.mTitle = title;
            return this;
        }

        @Override
        public Builder setTitle(int titleId) {
            P.mTitle = getContext().getString(titleId);
            return this;
        }

        @Override
        public Builder setMessage(CharSequence message) {
            P.mMessage = message;
            return this;
        }

        @Override
        public Builder setMessage(int messageId) {
            P.mMessage = getContext().getString(messageId);
            return this;
        }

        @Override
        public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
            P.mPositiveText = text;
            P.mPositiveListener = v -> listener.onClick(dialog, 0);
            return this;
        }

        @Override
        public Builder setPositiveButton(int textId, final OnClickListener listener) {
            P.mPositiveText = getContext().getString(textId);
            P.mPositiveListener = v -> listener.onClick(dialog, 0);
            return this;
        }

        @Override
        public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
            P.mNegativeText = text;
            P.mNegativeListener = v -> listener.onClick(dialog, 0);
            return this;
        }

        @Override
        public Builder setNegativeButton(int textId, final OnClickListener listener) {
            P.mNegativeText = getContext().getString(textId);
            P.mNegativeListener = v -> listener.onClick(dialog, 0);
            return this;
        }

        @Override
        public CheckAlertDialog create() {
            dialog = new CheckAlertDialog(getContext());
            dialog.setCancelable(P.mCancelable);
            if (P.mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            return dialog;
        }

        @Override
        public CheckAlertDialog show(){
            create().show();

            Window window = dialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setContentView(R.layout.dialog_check);
            window.setLayout(DisplayUtil.dp2px(300), LinearLayout.LayoutParams.WRAP_CONTENT);

            LinearLayout llRoot = dialog.findViewById(R.id.ll_root);
            llRoot.setClipToOutline(true);
            TextView tvTitle = dialog.findViewById(R.id.tv_title);
            tvTitle.setText(P.mTitle);
            TextView tvContent = dialog.findViewById(R.id.tv_content);
            tvContent.setText(P.mMessage);
            TextView tvOK = dialog.findViewById(R.id.tv_ok);
            tvOK.setText(P.mPositiveText);
            tvOK.setOnClickListener(P.mPositiveListener);
            TextView tvCancel = dialog.findViewById(R.id.tv_cancel);
            tvCancel.setText(P.mNegativeText);
            tvCancel.setOnClickListener(P.mNegativeListener);

            return dialog;
        }
    }

    protected CheckAlertDialog(Context context) {
        super(context);
    }
}
